const username=document.querySelector('#username')
const password=document.querySelector('#password')
const btn=document.querySelector('button')
const userid=document.querySelector('.user-name')
const check=document.querySelector('.checkbox')
btn.onclick=function () {
    if (check.checked==false){
        alert("请您同意用户条款")
    }
    else {
    const xhr=new XMLHttpRequest()
    xhr.open('POST','http://43.138.138.11:1110/api/users/login',true)
    xhr.setRequestHeader('Content-Type','application/json')
    const params={
        username:username.value,
        password:password.value,
    }
    var name=username.value
    xhr.send(JSON.stringify(params))
    xhr.onreadystatechange=function () {
        if (xhr.readyState===4&&xhr.status===200){
            var text=JSON.parse(xhr.responseText)
             if (text.msg==='操作成功'){
                 alert("登陆成功！")
                 location.href='index.html'
                 console.log(xhr.responseText)
                 window.localStorage.setItem('username',name)
                 window.localStorage.setItem('userid',text.data.userId)
                 console.log(userid)
             }
             else{
                 alert(text.msg)
                 self.location='login.html'
             }
        }
    }
    }
}
